import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
import unittest


class MyTestCase(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        service = Service(executable_path="../driver/geckodriver.exe")
        cls.driver = webdriver.Firefox(service=service)

    def test_navigate(self):
        """
        测试导航对象
        :return:
        """
        self.driver.get("https://www.baidu.com")
        text = self.driver.find_element(By.XPATH, "//input[@id='kw']")
        # text.send_keys("selenium", Keys.RETURN)  # 点击回车
        text.send_keys("selenium")

        # 通过  xpath 的方式进行元素 定位
        # button = self.driver.find_element(By.XPATH, "//input[@id='su']")

        # 通过  Css 的方式 来进行 元素 定位
        # button = self.driver.find_element(By.CSS_SELECTOR, "input[id='su']")
        button = self.driver.find_element(By.CSS_SELECTOR, "input#su")
        button.click()

        # 可以  通过 导航对象 对我们的网页进行 回退操作
        self.driver.back()
        time.sleep(3)

        # 前进
        self.driver.forward()
        time.sleep(3)

        # 刷新
        self.driver.refresh()

    @classmethod
    def tearDownClass(cls) -> None:
        # cls.driver.close()
        # cls.driver.quit()
        pass


if __name__ == '__main__':
    unittest.main()
